package com.mvc.util;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mvc.annocation.ActionMapping;
import com.mvc.annocation.Controller;
import com.mvc.config.ActionForward;
import com.mvc.config.ConfigBean;
import com.mvc.config.XmlBean;
import com.mvc.listener.MvcConfigListener;

public class AnnocationParse {
	private static String classPath = "";
	private static String scanPkg = "";
	
	public AnnocationParse(String scanPkg){
		this.classPath = AnnocationParse.class.getResource("/").getPath();
		this.scanPkg = scanPkg;
	}
	
	public Map<String, ConfigBean> getConfigBeans(){
		String scanPath = classPath + scanPkg.replaceAll("\\.", "\\/");
		Map<String, ConfigBean> beans = new HashMap<String, ConfigBean>();
		scanFile(beans, new File(scanPath), scanPkg);
		return beans;
	}
	
	private void scanFile(Map<String, ConfigBean> beans, File file, String pkg){
		if(file.isDirectory()){
			File[] fList = file.listFiles();
			for(File f:fList){
				if(f.isDirectory()){
					scanFile(beans,f,pkg + "." + f.getName());
				}else{
					parseFile(beans, f, pkg);
				}
			}
		}else if(file.isFile()){
			parseFile(beans, file, pkg);
		}
	}

	private void parseFile(Map<String, ConfigBean> beans, File file, String pkg) {
		String fileName = file.getName();
		if(fileName.endsWith(".class")){
			fileName = fileName.replace(".class", "");
			try {
				System.out.println("pkg==" + pkg + "." + fileName);
				Class<?> clazz = Class.forName(pkg + "." + fileName);
				if(clazz.isAnnotationPresent(Controller.class)){
					Controller controller = clazz.getAnnotation(Controller.class);
					System.out.println("加载action:" + controller.action());
					ConfigBean bean = new ConfigBean();
					bean.setAction(controller.action());
					bean.setActionClass(clazz);
					bean.setForm(controller.form());
					
					Map<String, Map<String, ActionForward>> map = new HashMap<String, Map<String, ActionForward>>();
					Method[] methods = clazz.getMethods();
					for(Method m:methods){
						if(m.isAnnotationPresent(ActionMapping.class)){
							ActionMapping mapping = m.getAnnotation(ActionMapping.class);
							System.out.println("加载action:" + controller.action() + " 的方法：" + mapping.path());
							ActionForward success = new ActionForward();
							success.setResultName("success");
							success.setResultUrl(mapping.successUrl());
							
							ActionForward fail = new ActionForward();
							fail.setResultName("fail");
							fail.setResultUrl(mapping.failUrl());
							
							Map<String, ActionForward> fMap = new HashMap<String, ActionForward>();
							fMap.put("success", success);
							fMap.put("fail", fail);
							map.put(mapping.path(), fMap);
						}
					}
					bean.setMethod(map);
					beans.put(controller.action(),bean);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new AnnocationParse("demo.action").getConfigBeans();
	}
}
